package com.deer.wms.system.manage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.system.manage.dao.DeptMapper;
import com.deer.wms.system.manage.model.dept.Dept;
import com.deer.wms.system.manage.model.dept.DeptCriteria;
import com.deer.wms.system.manage.model.dept.DeptDto;
import com.deer.wms.system.manage.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author guo
 * @since 2019/11/05
 */
@Service
@Transactional
public class DeptServiceImpl extends SuperServiceImpl<DeptMapper, Dept> implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private DeptService deptService;

    @Override
    public Result createDept(DeptDto dept, CurrentUser currentUser) {
        if (currentUser == null) {
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        //校验是否已经有对应的部门及部门名称存在
        List<DeptDto> list1 = deptService.findByDeptCode(dept.getDeptCode());
        List<DeptDto> list2 = deptService.findByDeptName(dept.getDeptName());

        if (list1.size() != 0) {
            throw new ServiceException(CommonCode.DEPT_EXISTS, "此部门编码已存在");
        }
        if (list2.size() != 0) {
            throw new ServiceException(CommonCode.DEPT_CODE_EXISTS, "此部门编码已存在");
        }

        //保存
        this.save(dept);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public List<Dept> findList(DeptCriteria criteria) {
        return list(new QueryWrapper<Dept>()
                .eq("enabled", 1)
                .like(ObjectUtil.isNotEmpty(criteria.getKeyWords()), "dept_name", criteria.getKeyWords())
        );
    }

    @Override
    public List<DeptDto> findByDeptCode(String deptCode) {
        return deptMapper.findByDeptCode(deptCode);
    }

    @Override
    public List<DeptDto> findByDeptName(String deptName) {
        return deptMapper.findByDeptName(deptName);
    }

    @Override
    public List<Dept> findByParentId(Integer parentId) {
        return deptMapper.findByParentId(parentId);
    }

    @Override
    public List<Integer> treeMenuList(List<DeptDto> list, Integer parentId) {
        List<Integer> newList = new ArrayList<Integer>();
        for (DeptDto deptDto : list) {
            //遍历出父id等于参数的id，add进子节点集合
            if (deptDto.getParentId().equals(parentId)) {
                //递归遍历下一级
                // list.remove(itemType);
                treeMenuList(list, deptDto.getDeptId());
                newList.add(deptDto.getDeptId());
            }
        }
        return newList;
    }

    @Override
    public void deleteByCodeAndCom(DeptCriteria criteria) {
        deptMapper.deleteByCodeAndCom(criteria);
    }

    /**
     * 部门树表
     *
     * @param pid
     * @return
     */
    @Override
    public List<Dept> treeDept(Integer pid) {
        List<Dept> list = list();

        //过滤
        List<Dept> collect = list.parallelStream().filter(categoryEntity -> categoryEntity.getParentId().equals(pid)).map(menu -> {
            //setChildren
            menu.setChildren(getChildrens(menu, list));
            return menu;
            //排序
        }).collect(Collectors.toList());
        return collect;
    }
    public List<Dept> getChildrens(Dept entity, List<Dept> all) {
        //过滤
        List<Dept> collect = all.parallelStream().filter(categrotyEntity -> categrotyEntity.getParentId().equals(entity.getDeptId())).map(categoryEntity->{
            //setChildren
            categoryEntity.setChildren(this.getChildrens(categoryEntity, all));
            return categoryEntity;
            //排序
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Integer> treeParentList(List<Dept> list, Dept dept) {
        List<Integer> newList = new ArrayList<Integer>();
        for (Dept depts : list) {
            //遍历出父id等于参数的id，add进子节点集合
            Integer deptId = depts.getDeptId();
            //   Integer parentId = type.getParentId();
            if (dept.getParentId().equals(deptId)) {
                //递归遍历下一级
                // list.remove(itemType);
                treeParentList(list, depts);
                newList.add(deptId);
            }
        }
        newList.add(dept.getDeptId());
        return newList;
    }

}
